<?php

namespace App\Console\Commands;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class MakeModelCommand extends BaseCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'create:model';
    protected $targets = "*";
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = "Generate models from database schema";

    /**
     * @var \Illuminate\Filesystem\Filesystem
     */
    protected $files;


    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
        $this->files = new Filesystem();
        $targets = $this->argument('targets');
        $this->formatTargets($targets);
        $tables = $this->loadTables();
        foreach ($tables as $table) {
            $this->makeOneTable($table);
        }

    }

    protected function makeOneTable($tableName)
    {
        $meta = $this->getMetaData($tableName);
        $this->doGenerate($tableName, $meta[$tableName]);

    }

    protected function doGenerate($table, $tableInfo)
    {
        $skipPattern = $this->input->getOption('skip');
        if (!empty($skipPattern) && preg_match("/$skipPattern/", $table)) {
            echo "skip table: " . $table . "\n";
            return;
        }

        $basePath = app()->basePath();
        $class = Str::studly(Str::singular($table));

        $this->files->makeDirectory($basePath . "/app/Models/Auto/", 0755, true, true);
        $baseModelPath = $basePath . "/app/Models/Auto/Base" . $class . ".php";
        $modelPath = $basePath . "/app/Models/" . $class . ".php";

        $data['class'] = $class;
        $data['table'] = $table;
        $data['tableInfo'] = $tableInfo;
        $data['dollar'] = "$";
        $data['timestamps'] = "false";
        $data['increment'] = "false";

        foreach ($tableInfo['columns'] as $column) {
            // print_r($column);
            if ($column['name'] == "updated_at") {
                $data['timestamps'] = "false";  // 统一由控制器设置，否则表里面如果只有一个updated_at 字段会出错
            }
            if ($column['extra'] == "auto_increment") {
                $data['increment'] = "true";
            }
        }

        $content = view('stubs/base-model', $data);
        $this->files->put($baseModelPath, $content);

        if (!$this->files->exists($modelPath)) {
            $content = view('stubs/model', $data);
            $this->files->put($modelPath, $content);
        }

        echo $class . " generated\n";
    }




    protected function getArguments()
    {
        return [
            ['targets', InputArgument::OPTIONAL, 'specify the targets'],
        ];
    }

    protected function getOptions()
    {
        return [
            ['skip', "s", InputOption::VALUE_OPTIONAL, 'skip tables matches with the rule'],
        ];
    }
}